import { reactive } from "vue";
import { GJ_nowTime } from "../../../../static/js/tool.js";
import { Api } from "../../../api/api.js";
import { ElNotification } from 'element-plus';
function sendEmailJS() {
    let userinfo = JSON.parse(window.sessionStorage.getItem("userinfo"));
    let status = reactive({
        sendData: {
            sendUser: userinfo.user,
            core: "",
            addressee:"",
            title: "",
            content: "",
            reply: "",
            createTime: ""
        },
    });
    let methodFun = {
        //发送邮件
        submitFun: () => {
            status.sendData.createTime = GJ_nowTime("date-time", "-");
            console.log(status.sendData);
            let result = true;
            for (let key in status.sendData) {
                if (!status.sendData[key]) {
                    result = false;
                }
            }
            if (result) {
                if(status.sendData.sendUser !=status.sendData.addressee ){
                    Api.sendEmail(status.sendData).then((res) => {
                        if (res.data) {
                            // methodFun.init();
                            ElNotification({
                                type: "success",
                                title: "操作成功",
                                message: "邮件已成功发送",
                                duration: 1000,
                            });
                        }
                    })
                }else{
                    ElNotification({
                        type: "error",
                        title: "操作有误",
                        message: "不能将邮件发送给自己哦",
                        duration: 1000,
                    });
                }
            } else {
                ElNotification({
                    type: "error",
                    title: "发送失败",
                    message: "信息填写不完整！请检查后提交",
                    duration: 1000,
                });
            }
        },
        init:()=>{
            status.sendData.core ="";
            status.sendData.title ="";
            status.sendData.content ="";
            status.sendData.reply ="";
            status.sendData.createTime ="";
            status.sendData.addressee ="";
        }
    };
    return {
        status,
        methodFun
    }
}

export default sendEmailJS;